---
title: Avoid Public Fields
description: The avoid_public_fields rule.
---

import { Badge } from '@astrojs/starlight/components';
import EnableRuleSnippet from '~/components/lint-rules/EnableRuleSnippet.astro';
import BadSnippet from '~/components/lint-rules/avoid_public_fields/BadSnippet.mdx';
import GoodSnippet from '~/components/lint-rules/avoid_public_fields/GoodSnippet.astro';

<div class="badges">
	<Badge text="new" />
	<Badge text="dart" variant="note" />
	<Badge text="recommended" variant="success" />
</div>

Avoid exposing public fields on `Bloc` and `Cubit` instances.

## Rationale

Business logic components maintain their own `state` and emit state changes via
the `emit` API. As a result, all public facing state should be exposed via the
`state` object.

## Examples

**DO NOT** expose public fields on bloc and cubit instances.

**BAD**:

<BadSnippet />

**GOOD**:

<GoodSnippet />

## Enable

To enable the `avoid_public_fields` rule, add it to your `analysis_options.yaml`
under `bloc` > `rules`:

<EnableRuleSnippet name="avoid_public_fields" />
